﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Services;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HiViLicenseManage
{
	public partial class Register : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				if (!IsPostBack)
				{
					LoadInfomation();
				}
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}

		private void LoadInfomation()
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT SoftwareID, SoftwareName FROM Software WHERE Status = 'True'", appConn);
			sqlComm.CommandType = CommandType.Text;			
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtSoftware = new DataTable();
			sqlDA.Fill(dtSoftware);

			cklSoftware.DataSource = dtSoftware;
			cklSoftware.DataValueField = "SoftwareID";
			cklSoftware.DataTextField = "SoftwareName";
			cklSoftware.DataBind();

			appConn.Close();
		}

		private bool CheckExistAgentID(string agentID)
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT AgentID  FROM Agent WHERE UPPER(AgentID) = @AgentID", appConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@AgentID", SqlDbType.VarChar).Value = agentID.ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtAgent = new DataTable();
			sqlDA.Fill(dtAgent);

			appConn.Close();

			if (dtAgent.Rows.Count > 0)
				return true;
			else
				return false;
		}

		private bool CheckExistEmail(string email)
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT Email FROM Agent WHERE UPPER(Email) = @Email", appConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@Email", SqlDbType.VarChar).Value = email.ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtEmail = new DataTable();
			sqlDA.Fill(dtEmail);

			appConn.Close();

			if (dtEmail.Rows.Count > 0)
				return true;
			else
				return false;
		}

		protected void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				bool check = true;
				if (CheckExistAgentID(txtAgentID.Text.Trim()))
				{
					check = false;
					wrnAgentExist.InnerText = "  Tài khoản đã được đăng kí";
				}
				else
				{
					wrnAgentExist.InnerText = "";
				}
				if (CheckExistEmail(txtEmail.Text.Trim()))
				{
					check = false;
					wrnEmailExist.InnerText = "  Email đã được đăng kí";
				}
				else
				{
					wrnEmailExist.InnerText = "";
				}

				if (check == true)
				{
					SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
					appConn.Open();

					SqlCommand sqlComm = new SqlCommand("INSERT INTO Agent (AgentID, Password, AgentName, Company, Phone, Email, Address, DateCreated, Status) VALUES (@AgentID, @Password, @AgentName, @Company, @Phone, @Email, @Address, @DateCreated, @Status)", appConn);
					sqlComm.CommandType = CommandType.Text;
					sqlComm.Parameters.Add("@AgentID", SqlDbType.VarChar).Value = txtAgentID.Text.Trim().ToUpper();
					sqlComm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Enc.Encrypt(txtPassword.Text.Trim(), txtAgentID.Text.Trim().ToUpper());
					sqlComm.Parameters.Add("@AgentName", SqlDbType.NVarChar).Value = txtAgentName.Text.Trim();
					sqlComm.Parameters.Add("@Company", SqlDbType.NVarChar).Value = txtCompany.Text.Trim();
					sqlComm.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtPhone.Text.Trim();
					sqlComm.Parameters.Add("@Email", SqlDbType.VarChar).Value = txtEmail.Text.Trim();
					sqlComm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text.Trim();
					sqlComm.Parameters.Add("@DateCreated", SqlDbType.DateTime).Value = DateTime.Now;
					sqlComm.Parameters.Add("@Status", SqlDbType.Bit).Value = false;
					sqlComm.ExecuteNonQuery();

					foreach (ListItem item in cklSoftware.Items)
					{
						if (item.Selected)
						{
							SqlCommand sqlComm2 = new SqlCommand("INSERT INTO AgentKey (FK_AgentID, FK_SoftwareID, NumberKey, Status) VALUES (@FK_AgentID, @FK_SoftwareID, @NumberKey, @Status)", appConn);
							sqlComm2.CommandType = CommandType.Text;
							sqlComm2.Parameters.Add("@FK_AgentID", SqlDbType.VarChar).Value = txtAgentID.Text.Trim().ToUpper();
							sqlComm2.Parameters.Add("@FK_SoftwareID", SqlDbType.VarChar).Value = item.Value;
							sqlComm2.Parameters.Add("@NumberKey", SqlDbType.Int).Value = 3;
							sqlComm2.Parameters.Add("@Status", SqlDbType.Bit).Value = false;
							sqlComm2.ExecuteNonQuery();
						}
					}

					appConn.Close();
				}
				if (check == true)
					Response.Redirect("~/RegisterSucsess.aspx", false);
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}
	}
}
